package cn.meye11.crm.mapper;

import cn.meye11.crm.common.DateUtils;
import cn.meye11.crm.common.RegexUtils;
import cn.meye11.crm.common.StringUtils;
import cn.meye11.crm.domain.entity.RechargeRecordEntity;
import cn.meye11.crm.domain.query.RechargeRecordQuery;
import org.apache.ibatis.jdbc.SQL;

/**
 * 充值记录表(RechargeRecord)表数据库访问层 SQL组合类
 * 
 * @author makejava
 * @since 2023-11-23 20:16:08
 */
 
public class RechargeRecordProvider {

    public String list(RechargeRecordQuery query) {

        SQL sql = new SQL();
        sql.SELECT("*");
        sql.FROM(RechargeRecordEntity.Table + " rec ");
        sql.WHERE(wheres(query));
        sql.ORDER_BY(query.getSort() + " " + query.getDirection());
        return sql.toString();
    }

    public String count(RechargeRecordQuery query) {

        SQL sql = new SQL();
        sql.SELECT("count (id)");
        sql.FROM(RechargeRecordEntity.Table + " rec ");
        sql.WHERE(wheres(query));
        return sql.toString();
    }

    private String wheres(RechargeRecordQuery query) {
        StringBuilder where = new StringBuilder();
        if (query != null) {
            // 不为空时，必须为逗号分割的数字才能查询
            String id = StringUtils.commaDelimitedTrimEmptyString(query.getId());
            if (StringUtils.isNotBlank(id) && RegexUtils.check(RegexUtils.Type.ONLY_NUMBER_OR_EMPTY, id)) {
                where.append("rec.id in (" + id + ")").append(" and ");
            }
            if (query.getMemberId() != null) {
                where.append("rec.member_id = " + query.getMemberId()).append(" and ");
            }
            if (query.getUserId() != null) {
                where.append("rec.user_id = " + query.getUserId()).append(" and ");
            }
            if (query.getAmount() != null) {
                where.append("rec.amount = " + query.getAmount()).append(" and ");
            }
            if (query.getGiftAmount() != null) {
                where.append("rec.gift_amount = " + query.getGiftAmount()).append(" and ");
            }
            if (StringUtils.isNotBlank(query.getRechargeType())) {
                where.append("rec.recharge_type = '" + query.getRechargeType() + "'").append(" and ");
            }
            if (StringUtils.isNotBlank(query.getRechargeTypeLike())) {
                where.append("rec.recharge_type like '%" + query.getRechargeTypeLike() + "%'").append(" and ");
            }
            String rechargeTypeIn = StringUtils.toSqlInString(query.getRechargeTypeIn());
            if (StringUtils.isNotBlank(rechargeTypeIn)) {
                where.append("rec.recharge_type in (" + rechargeTypeIn + ")").append(" and ");
            }
            String rechargeTypeNotIn = StringUtils.toSqlInString(query.getRechargeTypeNotIn());
            if (StringUtils.isNotBlank(rechargeTypeNotIn)) {
                where.append("rec.recharge_type not in (" + rechargeTypeNotIn + ")").append(" and ");
            }
            if (query.getRechargeTime() != null) {
                where.append("rec.recharge_time = '" + query.getRechargeTime()).append("' and ");
            }
            if (StringUtils.isNotBlank(query.getOperators())) {
                where.append("rec.operators = '" + query.getOperators() + "'").append(" and ");
            }
            if (StringUtils.isNotBlank(query.getOperatorsLike())) {
                where.append("rec.operators like '%" + query.getOperatorsLike() + "%'").append(" and ");
            }
            String operatorsIn = StringUtils.toSqlInString(query.getOperatorsIn());
            if (StringUtils.isNotBlank(operatorsIn)) {
                where.append("rec.operators in (" + operatorsIn + ")").append(" and ");
            }
            String operatorsNotIn = StringUtils.toSqlInString(query.getOperatorsNotIn());
            if (StringUtils.isNotBlank(operatorsNotIn)) {
                where.append("rec.operators not in (" + operatorsNotIn + ")").append(" and ");
            }
            if (StringUtils.isNotBlank(query.getLabel())) {
                where.append("rec.label = '" + query.getLabel() + "'").append(" and ");
            }
            if (StringUtils.isNotBlank(query.getLabelLike())) {
                where.append("rec.label like '%" + query.getLabelLike() + "%'").append(" and ");
            }
            String labelIn = StringUtils.toSqlInString(query.getLabelIn());
            if (StringUtils.isNotBlank(labelIn)) {
                where.append("rec.label in (" + labelIn + ")").append(" and ");
            }
            String labelNotIn = StringUtils.toSqlInString(query.getLabelNotIn());
            if (StringUtils.isNotBlank(labelNotIn)) {
                where.append("rec.label not in (" + labelNotIn + ")").append(" and ");
            }
            if (query.getStart() != null) {
                where.append("rec.time >= '" + DateUtils.localDateTimeToStr(query.getStart()) + "'").append(" and ");
            }
            if (query.getEnd() != null) {
                where.append("rec.time <= '" + DateUtils.localDateTimeToStr(query.getEnd()) + "'").append(" and ");
            }
        }
        where.append("rec.del='0'");
        return where.toString();
    }
}
